<?php 

class DatabaseInterface {

	var $db;
	var $result = null;
	var $connected = false;
	
	var $db_ip;
	var $db_user;
	var $db_pass;
	var $db_name;

	function DatabaseInterface($db_ip, $db_user, $db_pass, $db_name, $displayErrors) {
		
		$this->db_ip			= $db_ip;
		$this->db_user			= $db_user;
		$this->db_pass			= $db_pass;
		$this->db_name			= $db_name;
		
		$this->displayErrors	= $displayErrors;
		
		if(!isset($this->db_ip)) return $this->handleError('db_ip is undefined. May be missing ' . $this->db_key . '.');
		
		if(!$this->db = @mysql_connect($this->db_ip, $this->db_user, $this->db_pass)) {
			return $this->handleError('Failed to connect to ' . $this->db_name . '.');
		}
		
		if(!mysql_select_db($this->db_name, $this->db)) return $this->handleError('Failed to select ' . $this->db_name . '.');
		
		$this->connected = true;
		
	}
	
	function handleError($error) {
		
		if($this->displayErrors) {
			
			echo "\n\n<!-- error: $error -->\n\n";
			echo "\n\n<!--" . mysql_errno( ) . ": " . mysql_error( ) . " -->\n\n";
			
			echo "<BR>\n<BR>\n";
			echo $error . "<BR>\n";
			echo $this->db_ip	. "<BR>\n";
			echo $this->db_user	. "<BR>\n";
			echo $this->db_pass . "<BR>\n";
			echo $this->db_name . "<BR>\n";
			echo "<BR>\n";
			
		}
		
		return false;
	}
	
	function query($sql) {
		
		if($this->displayErrors) {
			echo "$sql\n<BR>";
		}
		
		if(!$this->result = mysql_query($sql, $this->db)) return $this->handleError("SQL Failed: $sql");
		return true;
		
	}
	
	function getInsertId( ) {
		if($this->result == null) return null;
		else return mysql_insert_id( );
	}
	
	function getRow( ) {
		if($this->result == null) return null;
		if($row = mysql_fetch_array($this->result)) return $row;
		else return null;
	}
	
	function getNumRows( ) {
		if($this->result == null) return null;
		return mysql_num_rows($this->result);
	}
	
	function getAffectedRows( ) {
		if($this->result == null) return null;
		return mysql_affected_rows( );
	}
	
	function getFieldNames($table) {
		if(!$this->connected) return null;
		$fields		= mysql_list_fields($this->db_name, $table, $this->db);
		$columns	= mysql_num_fields($fields);

		for ($i = 0; $i < $columns; $i++) {
		    $fieldNames[$i] = mysql_field_name($fields, $i);
		}
		
		return $fieldNames;
	}
	
	function getFieldInfo($table) {
		if(!$this->connected) return null;
		
		$SQL = "SHOW FIELDS FROM $table";
		if($this->query($SQL)) {
			while($row = $this->getRow( )) {
				$field = $row['Field'];
				$fieldArray[$field] = $row;
			}
		}
		
		$fields		= mysql_list_fields($this->db_name, $table, $this->db);
		$columns	= mysql_num_fields($fields);
		
		for ($i = 0; $i < $columns; $i++) {
		    $name = mysql_field_name($fields, $i);
			$length = mysql_field_len($fields, $i);
			$flags = mysql_field_flags($fields, $i);
			
			$fieldArray[$name]["Length"] = $length;
			
			$flagNames = array(
				"not_null",
				"primary_key",
				"unique_key",
				"multiple_key",
				"blob",
				"unsigned",
				"zerofill",
				"binary",
				"enum",
				"auto_increment",
				"timestamp"
			);
			
			foreach($flagNames as $flag) {
				$fieldArray[$name][$flag] = (strstr($flags, $flag))	? true : false;
			}
			
			
		}
		
		return $fieldArray;
	
	}
	
	function isConnected( ) {
		return $this->connected;
	}
	
	function resetRows( ) {
		mysql_data_seek($this->result, 0);
	}
	
	function escapeString($str) {
		if(!$this->connected) return null;
		return mysql_real_escape_string($str, $this->db);
	}

}

?>